﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DALL.SysManage
{
    public class T_PUB_UNIALMATIAL
    {
        public DataTable GetList(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT u.*, \n");
            varname1.Append("       m.MATERIAL_NAME, \n");
            varname1.Append("       m.IMAGEURL, \n");
            varname1.Append("       d.NAME \n");
            varname1.Append("FROM   dbo.T_PUB_UNIALMATIAL u \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_MATERIAL m \n");
            varname1.Append("         ON u.Material = m.MATERIAL_ID \n");
            varname1.Append("       LEFT JOIN dbo.T_PUB_DICTIONARY d \n");
            varname1.Append("         ON u.WeedPrice = d.ID ");
            if (strWhere.Trim() != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int strid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_UNIALMATIAL ");
            strSql.Append(" where id=@id ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "id", DbType.Int32, strid);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_UNIALMATIAL GetModel(int strid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,Month,Material,Price,Other,WeedPrice from T_PUB_UNIALMATIAL ");
            strSql.Append(" where id=@id ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "id", DbType.Int32, strid);
            Model.SysManage.T_PUB_UNIALMATIAL model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }
        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_UNIALMATIAL ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_UNIALMATIAL model = new Model.SysManage.T_PUB_UNIALMATIAL();
            object ojb;
            ojb = dataReader["id"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.id = (int)ojb;
            }
            model.Month = dataReader["Month"].ToString();
            ojb = dataReader["Material"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Material = (int)ojb;
            }
            ojb = dataReader["Price"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Price =Convert.ToDecimal(ojb);
            }
            model.Other = dataReader["Other"].ToString();
            model.WeedPrice = dataReader["WeedPrice"].ToString();
            return model;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_UNIALMATIAL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_UNIALMATIAL(");
            strSql.Append("Month,Material,Price,Other,WeedPrice)");

            strSql.Append(" values (");
            strSql.Append("@Month,@Material,@Price,@Other,@WeedPrice)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "Month", DbType.String, model.Month);
            db.AddInParameter(dbCommand, "Material", DbType.Int32, model.Material);
            db.AddInParameter(dbCommand, "Price", DbType.Double, model.Price);
            db.AddInParameter(dbCommand, "Other", DbType.String, model.Other);
            db.AddInParameter(dbCommand, "WeedPrice", DbType.String, model.WeedPrice);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_UNIALMATIAL model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_UNIALMATIAL set ");
            strSql.Append("Month=@Month,");
            strSql.Append("Material=@Material,");
            strSql.Append("Price=@Price,");
            strSql.Append("Other=@Other,");
            strSql.Append("WeedPrice=@WeedPrice");
            strSql.Append(" where id=@id ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "id", DbType.Int32, model.id);
            db.AddInParameter(dbCommand, "Month", DbType.String, model.Month);
            db.AddInParameter(dbCommand, "Material", DbType.Int32, model.Material);
            db.AddInParameter(dbCommand, "Price", DbType.Double, model.Price);
            db.AddInParameter(dbCommand, "Other", DbType.String, model.Other);
            db.AddInParameter(dbCommand, "WeedPrice", DbType.String, model.WeedPrice);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
